<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2019/2/17
 * Time: 16:00
 */

namespace app\test\controller;

use app\common\lib\astrolog\Astrolog;
use app\common\lib\exception\ApiException;
use app\common\model\AstroSettingUser1751;
use app\common\model\Recode;
use app\common\model\User;
use Exception;
use think\Controller;
use think\Log;

/**
 * 自动化测试星盘数据和本地数据
 * Class Astrolabe
 * @package app\api\controller\v8
 */
class TestAstro extends Controller
{

    protected $type = '';
    private $user = [];

    public function index()
    {
        if (!$this->request->isPost())
        {
            return show(config('code.error'), '未知操作');
        }

        $param = $this->request->only(['json', 'chart', 'tid', 'id', 'uid', 'datetime', 'latitude', 'longitude', 'timezone', 'id1', 'id2']);

        $param['json'] = strip_tags(((string)$param['json']));
        $param['chart'] = (string)$param['chart'];
        $param['tid'] = (int)$param['tid'];
        $param['id'] = isset($param['id']) ? (int)$param['id'] : 0;
        $param['id1'] = isset($param['id1']) ? (int)$param['id1'] : 0;
        $param['id2'] = isset($param['id2']) ? (int)$param['id2'] : 0;
        $param['uid'] = (int)$param['uid'];
        $param['datetime'] = (string)$param['datetime'];
        if ($param['datetime'] && strtotime($param['datetime']) === false)
        {
            return show(config('code.error'), '时间参数错误');
        }
        $param['latitude'] = isset($param['latitude']) ? (float)$param['latitude'] : 0;
        $param['longitude'] = isset($param['longitude']) ? (float)$param['longitude'] : 0;
        $param['timezone'] = isset($param['timezone']) ? (float)$param['timezone'] : 0;


        try
        {
            $userinfo = (new User())->getUserinfo(['id' => $param['uid']]);
            $this->user = $userinfo;
            //生成服务器文件
            $this->requestServerJson($param);
        }
        catch (ApiException $e)
        {
            return show(config('code.error'), '保存服务器json文件错误');
        }

        //生成本地化文件
        $this->createSettingFile($param['chart'] . '_local.json', $param['json']);

        $local_data = json_decode($param['json'], true);
        if ($local_data['code'] != 200)
        {
            return show(config('code.error'), $local_data['msg'], '');
        }

        $result = array();
        //对比json
        $this->compareJson($param['chart'], $result, $param['chart']);

        if (0 == count($result))
        {
            return show(config('code.success'), '数据一致', $result);
        } else
        {
            return show(config('code.error'), '数据不一致', $result);
        }

    }

    /**
     * 对比json文件
     * @param $type
     * @param $result
     * @param string $chart
     */
    private function compareJson($type, &$result, $chart = '')
    {
        try
        {
            $server_json_file = RUNTIME_PATH . 'astro' . DS . $type . '_server.json';
            $local_json_file = RUNTIME_PATH . 'astro' . DS . $type . '_local.json';
            if (file_exists($server_json_file) && file_exists($local_json_file))
            {
                $data_server = json_decode(file_get_contents($server_json_file), true);
                $data_local = json_decode(file_get_contents($local_json_file), true);

                if (isset($data_server['data']) && isset($data_local['data']))
                {
                    $index = 0;
                    if (isset($data_local['data']['planets']) && isset($data_server['data']['planets']))
                    {
                        //单盘
                        $this->compareItemData($data_local['data'], $data_server['data'], $result, $index, $chart);
                        //日返、月返盘 比较返照时间
                        if ($chart === 'lunar_return' || $chart === 'solar_return')
                        {
                            if ($data_local['data']['return_time'] !== $data_server['data']['return_time'])
                            {
                                $result[$index][] = 'return_time is not same';
                                $result[$index][] = 'local data return_time=' . $data_local['data']['return_time'];
                                $result[$index][] = 'server data return_time=' . $data_server['data']['return_time'];
                                $index++;
                            }
                        }

                    } else if (!isset($data_local['data']['planets']) && !isset($data_server['data']['planets']))
                    {
                        //双盘
                        $this->compareItemData($data_local['data'][0], $data_server['data'][0], $result, $index, $chart);
                        $this->compareItemData($data_local['data'][1], $data_server['data'][1], $result, $index, $chart);
                    } else
                    {
                        $result[] = '错误，data类型不一致';
                    }
                } else
                {
                    $result[] = '数据错入，没有data字段';
                }


                //*******递归**********
//                $deep_count = 0;
//                $key_params = [];
//                $parent_index = 0;
//                $this->repeatMethod($deep_count, $key_params, $parent_index, $data_server, $data_local, $result);
                //*******递归**********
            }


        }
        catch (Exception $e)
        {
            Log::write('对比json文件' . $e->getMessage(), 'error');
            $result[] = $e->getMessage();
        }
    }

    /**
     * 比较单条数据
     * @param array $local_data
     * @param array $server_data
     * @param array $result
     * @param int $index
     * @param string $chart
     */
    private function compareItemData($local_data = [], $server_data = [], &$result = [], &$index = 0, $chart = '')
    {

        //对上帝手指进行排序
        if (isset($local_data['structure']['asterism']['3']))
        {
            foreach ($local_data['structure']['asterism']['3'] as $k => $v)
            {
                //服务器和本地按照id排序（因为id不会重复）
                $local_data['structure']['asterism']['3'][$k] = resort($v, 'id');
                $server_data['structure']['asterism']['3'][$k] = resort($v, 'id');
            }
        }

        //对 力量分布->续宫、角宫、果宫 排序
//        if (isset($server_data['influence']['p'])){
//
//            $tmp_data_server['angular']=$server_data['influence']['p']['angular'];
//            $tmp_data_server['apoklimas']=$server_data['influence']['p']['apoklimas'];
//            $tmp_data_server['continues']=$server_data['influence']['p']['continues'];
//            $server_data['influence']['p']=$tmp_data_server;
//
//            $tmp_data_local['angular']=$local_data['influence']['p']['angular'];
//            $tmp_data_local['apoklimas']=$local_data['influence']['p']['apoklimas'];
//            $tmp_data_local['continues']=$local_data['influence']['p']['continues'];
//            $local_data['influence']['p']=$tmp_data_local;
//        }


        foreach ($server_data as $server_key => $server_value)
        {
            if (!$server_value || 'time1' == $server_key)
            {
                continue;
            }
            $local_value = $local_data[$server_key];

            if ('planets' == $server_key)
            {
                foreach ($server_value as $server_planet_key1 => $server_planet_value1)
                {
                    if (isset($local_value[$server_planet_key1]))
                    {
                        $local_planet_value = $local_value[$server_planet_key1];
                    } else
                    {
                        $result[$index][] = $server_key;
                        $result[$index][] = $server_planet_key1;
                        $result[$index][] = 'local data not contain ' . $server_planet_key1;
                        $index++;
                        continue;
                    }

                    if ($server_planet_value1 != $local_planet_value)
                    {
                        foreach ($server_planet_value1 as $server_planet_key2 => $server_planet_value2)
                        {
                            $local_planet_value2 = $local_planet_value[$server_planet_key2];
                            //古典参数只在天象、本命、12、13分、福点盘对比
                            if ("ancient" == $server_planet_key2 && !in_array($chart, ['now', 'natal', 'dodekatemorion', 'trimsamsa', 'fortune']))
                                continue;
                            if ($server_planet_value2 != $local_planet_value2)
                            {
                                if (is_array($server_planet_value2))
                                {
                                    foreach ($server_planet_value2 as $server_planet_key3 => $server_planet_value3)
                                    {
                                        if ('star' == $server_planet_key3 || 'plus' == $server_planet_key3)
                                            continue;
                                        if (isset($local_planet_value2[$server_planet_key3]))
                                        {
                                            $local_planet_value3 = $local_planet_value2[$server_planet_key3];
                                        } else
                                        {
                                            $result[$index][] = $server_key;
                                            $result[$index][] = $server_planet_key1;
                                            $result[$index][] = $server_planet_key2;
                                            $result[$index][] = $server_planet_key3;
                                            $result[$index][] = 'local data not contain ' . $server_planet_key3;
                                            $index++;
                                            continue;
                                        }
                                        if ($server_planet_value3 != $local_planet_value3)
                                        {
                                            if (is_array($server_planet_value3))
                                            {
                                                foreach ($server_planet_value3 as $server_planet_key4 => $server_planet_value4)
                                                {
                                                    $local_planet_value4 = $local_planet_value3[$server_planet_key4];

                                                    if ($server_planet_value4 != $local_planet_value4)
                                                    {
                                                        if (is_array($server_planet_value4))
                                                        {
                                                            foreach ($server_planet_value4 as $server_planet_key5 => $server_planet_value5)
                                                            {
                                                                if (0 == count($local_planet_value4))
                                                                {
                                                                    $result[$index][] = $server_key;
                                                                    $result[$index][] = $server_planet_key1;
                                                                    $result[$index][] = $server_planet_key2;
                                                                    $result[$index][] = $server_planet_key3;
                                                                    $result[$index][] = $server_planet_key4;
                                                                    $result[$index][] = $server_planet_key5;
                                                                    $result[$index][] = 'error:' . $server_planet_value5;
                                                                    $index++;
                                                                    continue;
                                                                }
                                                                $local_planet_value5 = $local_planet_value4[$server_planet_key5];

                                                                if ($server_planet_value5 != $local_planet_value5)
                                                                {
                                                                    if (is_array($server_planet_value5))
                                                                    {
                                                                        $result[$index][] = '还有数据没递归';
                                                                    } else
                                                                    {
                                                                        $result[$index][] = $server_key;
                                                                        $result[$index][] = $server_planet_key1;
                                                                        $result[$index][] = $server_planet_key2;
                                                                        $result[$index][] = $server_planet_key3;
                                                                        $result[$index][] = $server_planet_key4;
                                                                        $result[$index][] = $server_planet_key5;
                                                                        $result[$index][] = 'server=' . $server_planet_value5;
                                                                        $result[$index][] = 'local=' . $local_planet_value5;
                                                                        $index++;
                                                                    }
                                                                }
                                                            }
                                                        } else
                                                        {
                                                            if (is_numeric($server_planet_value4))
                                                            {
                                                                if (!$this->compareNum($server_planet_value4, $local_planet_value4))
                                                                {
                                                                    $result[$index][] = $server_key;
                                                                    $result[$index][] = $server_planet_key1;
                                                                    $result[$index][] = $server_planet_key2;
                                                                    $result[$index][] = $server_planet_key3;
                                                                    $result[$index][] = $server_planet_key4;
                                                                    $result[$index][] = 'server=' . $server_planet_value4;
                                                                    $result[$index][] = 'local=' . $local_planet_value4;
                                                                    $index++;
                                                                }
                                                            } else
                                                            {
                                                                $result[$index][] = $server_key;
                                                                $result[$index][] = $server_planet_key1;
                                                                $result[$index][] = $server_planet_key2;
                                                                $result[$index][] = $server_planet_key3;
                                                                $result[$index][] = $server_planet_key4;
                                                                $result[$index][] = 'server=' . $server_planet_value4;
                                                                $result[$index][] = 'local=' . $local_planet_value4;
                                                                $index++;
                                                            }

                                                        }
                                                    }
                                                }
                                            } else
                                            {
                                                if ("antipode" == $server_planet_key3)
                                                    continue;
                                                if ("ancient" == $server_planet_key2 && in_array($server_planet_key1, [7, 8, 9, 10]))
                                                    continue;
                                                $result[$index][] = $server_key;
                                                $result[$index][] = $server_planet_key1;
                                                $result[$index][] = $server_planet_key2;
                                                $result[$index][] = $server_planet_key3;
                                                $result[$index][] = 'server=' . $server_planet_value3;
                                                $result[$index][] = 'local=' . $local_planet_value3;
                                                $index++;
                                            }
                                        }
                                    }

                                } else
                                {
                                    if (is_numeric($server_planet_value2))
                                    {
                                        if (!$this->compareNum($server_planet_value2, $local_planet_value2))
                                        {
                                            $result[$index][] = $server_key;
                                            $result[$index][] = $server_planet_key1;
                                            $result[$index][] = $server_planet_key2;
                                            $result[$index][] = 'server=' . $server_planet_value2;
                                            $result[$index][] = 'local=' . $local_planet_value2;
                                            $index++;
                                        }
                                    }

                                }
                            }

                        }

                        $planet[] = $server_planet_key1;
                    }
                }
            }


            if ('houses' == $server_key)
            {
                foreach ($server_value as $server_house_key1 => $server_house_value1)
                {
                    $local_house_value1 = $local_value[$server_house_key1];
                    if ($server_house_value1 != $local_house_value1)
                    {
                        foreach ($server_house_value1 as $server_house_key2 => $server_house_value2)
                        {
                            $local_house_value2 = $local_house_value1[$server_house_key2];
                            if ($server_house_value2 != $local_house_value2)
                            {
                                if (is_array($server_house_value2))
                                {

                                    foreach ($server_house_value2 as $server_house_key3 => $server_house_value3)
                                    {
                                        if (isset($local_house_value2[$server_house_key3]))
                                        {
                                            $local_house_value3 = $local_house_value2[$server_house_key3];
                                        } else
                                        {
                                            $result[$index][] = $server_key;
                                            $result[$index][] = $server_house_key1;
                                            $result[$index][] = $server_house_key2;
                                            $result[$index][] = $server_house_key3;
                                            $result[$index][] = 'local data not contain ' . $server_house_key3;
                                            $index++;
                                            continue;
                                        }
                                        if ($server_house_value3 != $local_house_value3)
                                        {
                                            if (is_array($server_house_value3))
                                            {


                                                foreach ($server_house_value3 as $server_house_key4 => $server_house_value4)
                                                {
                                                    $local_house_value4 = $local_house_value3[$server_house_key4];
                                                    if ($server_house_value4 != $local_house_value4)
                                                    {
                                                        if (is_array($server_house_value4))
                                                        {
                                                            $result[$index][] = '还有数据没递归' . $server_house_key4;
                                                        } else
                                                        {
                                                            if ($this->compareNum($server_house_value4, $local_house_value4))
                                                                continue;
                                                            $result[$index][] = $server_key;
                                                            $result[$index][] = $server_house_key1;
                                                            $result[$index][] = $server_house_key2;
                                                            $result[$index][] = $server_house_key3;
                                                            $result[$index][] = $server_house_key4;
                                                            $result[$index][] = 'server=' . $server_house_value4;
                                                            $result[$index][] = 'local=' . $local_house_value4;
                                                            $index++;
                                                        }
                                                    }
                                                }

                                            } else
                                            {


                                                $result[$index][] = 'house->' . $server_house_key3;
                                            }
                                        }

                                    }

                                } else
                                {
                                    if ($this->compareNum($server_house_value2, $local_house_value2))
                                        continue;
                                    if ("protect_ancient_insign_deg" == $server_house_key2 || "in_sign_deg" == $server_house_key2 || "protect_insign_deg" == $server_house_key2)
                                    {
                                        $server_in_sign_deg_arr = explode('°', $server_house_value2);
                                        $local_in_sign_deg_arr = explode('°', $local_house_value2);
                                        if ($server_in_sign_deg_arr[0] == $local_in_sign_deg_arr[0])
                                        {
                                            continue;
                                        }
                                    }
                                    $result[$index][] = $server_key;
                                    $result[$index][] = $server_house_key1;
                                    $result[$index][] = $server_house_key2;
                                    $result[$index][] = 'server=' . $server_house_value2;
                                    $result[$index][] = 'local=' . $local_house_value2;
                                    $index++;
                                }
                            }
                        }
                    }
                }
            }

            $not_include_arabics_charts = ['composite', 'composite_thirdprogressed', 'composite_progressions', 'davison', 'marks', 'synastry_thirdprogressed', 'composite_thirdprogressed', 'composite_thirdprogressed', 'davison_progressions', 'synastry_progressions', 'davison_thirdprogressed'];

            if ('phase' == $server_key || ('arabics' == $server_key && !in_array($chart, $not_include_arabics_charts)) || 'star' == $server_key || 'fortune' == $server_key)
            {
                foreach ($server_value as $server_key1 => $server_value1)
                {
                    if ('star' == $server_key)
                    {
                        if (count($server_value) != count($local_value))
                        {
                            $result[$index][] = 'star 数量不一样';
                            $result[$index][] = 'server star count=' . count($server_value);
                            $result[$index][] = 'local star count=' . count($local_value);
                            $index++;
                            break;
                        }
                    }
                    if (isset($local_value[$server_key1]))
                    {
                        $local_value1 = $local_value[$server_key1];
                    } else
                    {
                        $result[$index][] = $server_key;
                        $result[$index][] = $server_key1;
                        $result[$index][] = 'local data not contain ' . $server_key1;
                        $index++;
                        continue;
                    }
                    if ($server_value1 != $local_value1)
                    {
                        if (is_array($server_value1))
                        {
                            foreach ($server_value1 as $server_key2 => $server_value2)
                            {
                                $local_value2 = $local_value1[$server_key2];
                                if ($server_value2 != $local_value2)
                                {
                                    if (is_array($server_value2))
                                    {
                                        foreach ($server_value2 as $server_key3 => $server_value3)
                                        {
                                            if (!isset($local_value2[$server_key3]))
                                            {
                                                $result[$index][] = $server_key;
                                                $result[$index][] = $server_key1;
                                                $result[$index][] = $server_key2;
                                                $result[$index][] = $server_key3;
                                                $result[$index][] = 'server=' . json_encode($server_value3);
                                                $result[$index][] = 'local=' . json_encode($local_value2);
                                                $index++;
                                                continue;
                                            }
                                            $local_value3 = $local_value2[$server_key3];
                                            if ($server_value3 != $local_value3)
                                            {
                                                if (is_array($server_value3))
                                                {
                                                    $result[$index][] = '数组没递归完' . $server_key3;
                                                } else
                                                {
                                                    $result[$index][] = $server_key;
                                                    $result[$index][] = $server_key1;
                                                    $result[$index][] = $server_key2;
                                                    $result[$index][] = $server_key3;
                                                    $result[$index][] = 'server=' . $server_value3;
                                                    $result[$index][] = 'local=' . $local_value3;
                                                    $index++;
                                                }
                                            }
                                        }
                                    } else
                                    {
                                        if ($this->compareNum($server_value2, $local_value2))
                                            continue;

                                        if ("in_sign_deg" == $server_key2)
                                        {
                                            $server_in_sign_deg_arr = explode('°', $server_value2);
                                            $local_in_sign_deg_arr = explode('°', $local_value2);
                                            if ($server_in_sign_deg_arr[0] == $local_in_sign_deg_arr[0])
                                            {
                                                continue;
                                            }
                                        }


                                        $result[$index][] = $server_key;
                                        $result[$index][] = $server_key1;
                                        $result[$index][] = $server_key2;
                                        $result[$index][] = 'server=' . $server_value2;
                                        $result[$index][] = 'local=' . $local_value2;
                                        $index++;
                                    }

                                }
                            }
                        } else
                        {
                            $result[$index][] = $server_key;
                            $result[$index][] = $server_key1;
                            $result[$index][] = 'server=' . $server_value1;
                            $result[$index][] = 'local=' . $local_value1;
                            $index++;
                        }
                    }
                }
            }


            if ('structure' == $server_key || 'characteristic' == $server_key || 'influence' == $server_key || 'according' == $server_key || 'firdaria' == $server_key || 'profection' == $server_key)
            {
                foreach ($server_value as $server_key1 => $server_value1)
                {

                    $local_value1 = $local_value[$server_key1];

                    if ($server_value1 != $local_value1)
                    {
                        if (is_array($server_value1))
                        {
                            foreach ($server_value1 as $server_key2 => $server_value2)
                            {
                                if (!isset($local_value1[$server_key2]))
                                {
                                    $result[$index][] = $server_key;
                                    $result[$index][] = $server_key1;
                                    $result[$index][] = $server_key2;
                                    $result[$index][] = 'server=' . json_encode($server_value2);
                                    $result[$index][] = 'local=' . json_encode($local_value1);
                                    $index++;
                                    continue;
                                }
                                $local_value2 = $local_value1[$server_key2];

                                if ($server_value2 != $local_value2)
                                {
                                    if (is_array($server_value2))
                                    {
                                        foreach ($server_value2 as $server_key3 => $server_value3)
                                        {
                                            if ('phase' == $server_key3 && '2' == $server_key1)
                                            {
                                                continue;
                                            }
                                            //福点盘不校验象限
                                            if ('p' == $server_key1 && $chart == 'fortune')
                                                continue;
                                            if (!isset($local_value2[$server_key3]))
                                            {
                                                $result[$index][] = $server_key;
                                                $result[$index][] = $server_key1;
                                                $result[$index][] = $server_key2;
                                                $result[$index][] = $server_key3;
                                                $result[$index][] = 'server=' . json_encode($server_value3);
                                                $result[$index][] = 'local=' . json_encode($local_value2);
                                                $index++;
                                                continue;
                                            }
                                            $local_value3 = $local_value2[$server_key3];
                                            if ($server_value3 != $local_value3)
                                            {
                                                if (is_array($server_value3))
                                                {
                                                    foreach ($server_value3 as $server_key4 => $server_value4)
                                                    {
                                                        $local_value4 = $local_value3[$server_key4];
                                                        if ($server_value4 != $local_value4)
                                                        {
                                                            if (is_array($server_value4))
                                                            {
                                                                if (is_array($server_value3))
                                                                {
                                                                    foreach ($server_value4 as $server_key5 => $server_value5)
                                                                    {
                                                                        $local_value5 = $local_value4[$server_key5];
                                                                        if ($server_value5 != $local_value5)
                                                                        {
                                                                            if (is_array($server_value5))
                                                                            {
                                                                                $result[$index][] = '未递归完' . $server_key5;
                                                                            } else
                                                                            {
                                                                                if ($this->compareNum($server_value5, $local_value5))
                                                                                    continue;

                                                                                $result[$index][] = $server_key;
                                                                                $result[$index][] = $server_key1;
                                                                                $result[$index][] = $server_key2;
                                                                                $result[$index][] = $server_key3;
                                                                                $result[$index][] = $server_key4;
                                                                                $result[$index][] = $server_key5;
                                                                                $result[$index][] = 'server=' . $server_value5;
                                                                                $result[$index][] = 'local=' . $local_value5;
                                                                                $index++;
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            } else
                                                            {
                                                                $result[$index][] = $server_key;
                                                                $result[$index][] = $server_key1;
                                                                $result[$index][] = $server_key2;
                                                                $result[$index][] = $server_key3;
                                                                $result[$index][] = $server_key4;
                                                                $result[$index][] = 'server=' . $server_value4;
                                                                $result[$index][] = 'local=' . $local_value4;
                                                                $index++;
                                                            }
                                                        }
                                                    }
                                                } else
                                                {
                                                    $result[$index][] = $server_key;
                                                    $result[$index][] = $server_key1;
                                                    $result[$index][] = $server_key2;
                                                    $result[$index][] = $server_key3;
                                                    $result[$index][] = 'server=' . $server_value3;
                                                    $result[$index][] = 'local=' . $local_value3;
                                                    $index++;
                                                }
                                            }
                                        }
                                    } else
                                    {
                                        if ($this->compareNum($server_value2, $local_value2))
                                            continue;

                                        if ("in_sign_deg" == $server_key2)
                                        {
                                            $server_in_sign_deg_arr = explode('°', $server_value2);
                                            $local_in_sign_deg_arr = explode('°', $local_value2);
                                            if ($server_in_sign_deg_arr[0] == $local_in_sign_deg_arr[0])
                                            {
                                                continue;
                                            }
                                        }

                                        $result[$index][] = $server_key;
                                        $result[$index][] = $server_key1;
                                        $result[$index][] = $server_key2;
                                        $result[$index][] = 'server=' . $server_value2;
                                        $result[$index][] = 'local=' . $local_value2;
                                        $index++;
                                    }

                                }
                            }
                        } else
                        {
                            if ($this->compareNum($server_value1, $local_value1))
                                continue;
                            $result[$index][] = $server_key;
                            $result[$index][] = $server_key1;
                            $result[$index][] = 'server=' . $server_value1;
                            $result[$index][] = 'local=' . $local_value1;
                            $index++;
                        }
                    }
                }
            }


        }
    }

    /**
     * 比较两个值误差在0.1之间，那么返回true，否则返回false；
     * @param int $value1
     * @param int $value2
     * @return bool
     */
    private function compareNum($value1 = 0, $value2 = 0)
    {
        if (is_numeric($value1) && is_numeric($value2) && ($value1 - 0.1) < $value2 && ($value1 + 0.1) > $value2)
        {
            return true;
        } else
            return false;
    }

    /**
     * 递归方法【废弃，因为我只对比数据就好，没必要写个递归】
     * @param int $deep_count
     * @param array $key_params
     * @param int $parent_index
     * @param array $server_arr
     * @param array $local_arr
     * @param array $result
     * @return mixed
     */
    private function repeatMethod(&$deep_count = 0, &$key_params = [], &$parent_index = 0, $server_arr = [], $local_arr = [], &$result = [])
    {
        if (is_array($server_arr) && is_array($local_arr))
        {
            foreach ($server_arr as $key => $value)
            {

                if ($value === $local_arr[$key])
                {//相同
//                    $result[20][] = $key;
                } else
                {//不相同
                    if (!is_array($server_arr[$key]) && !is_array($local_arr[$key]))
                    {
//                        if ($parent_index > 0)
//                        {
//                            $result[$parent_index] = $result[$parent_index - 1];
//                            //移除最后两个元素（最后不一样的key 和 该key下的server和local字段的值）
//                            array_pop($result[$parent_index]);
//                            array_pop($result[$parent_index]);
//                        }
//                        $result[$parent_index] = $key_params;
                        foreach ($key_params as $key_param => $value_param)
                        {
                            $result[$parent_index][] = $value_param;
                        }
//                        $result[$parent_index] = array_merge([], $key_params);

                        //不是数组，那么递归到了最后一级
                        $result[$parent_index][] = $key;
                        $result[$parent_index][] = 'server ="' . $server_arr[$key] . '",local="' . $local_arr[$key] . '"';
                        $parent_index++;
//                        $deep_count--;
//                        $is_add = 0;
                    } else
                    {
                        if ($deep_count < count($key_params))
                        {
                            $key_params = array_splice($key_params, 0, $deep_count - 1);
                            $key_params[] = $key;
                            $deep_count = count($key_params);
                        } else
                        {
                            $key_params[] = $key;
                            $deep_count++;
                        }

                        //是数组，那么继续递归，保留当前的key，方便查找
                        $this->repeatMethod($deep_count, $key_params, $parent_index, $value, $local_arr[$key], $result);
                    }


                }

            }
        }
//else{
//            $result[21][]=$key_param;
//        }
//
//        if (is_numeric($server_arr))
//        {
//
//        }
        return $result;
    }


    /**
     * 请求服务器json，并保存为本地文件
     * @param array $param
     * @throws ApiException
     */
    private function requestServerJson($param = [])
    {
        // 星盘配置方案使用默认值
        $tid = 1;

        (isset($param['tid']) && in_array($param['tid'], [1, 2, 3])) && $tid = $param['tid'];

        $settingData = (new AstroSettingUser1751())->getUserSettingByTid(['uid' => $this->user['id'], 'tid' => $tid, 'chart' => $param['chart']]);

        $Recode = new Recode();

        $Astrolog = Astrolog::getInstance();

        if (isset($param['id']) && $param['id'])
        {
            $Astrolog->setRecode1($Recode->getRecode(['id' => $param['id'], 'uid' => $this->user['id']]));
        }

        if (isset($param['id1']) && $param['id1'])
        {
            $Astrolog->setRecode1($Recode->getRecode(['id' => $param['id1'], 'uid' => $this->user['id']]));
        }

        if (isset($param['id2']) && $param['id2'])
        {
            $Astrolog->setRecode2($Recode->getRecode(['id' => $param['id2'], 'uid' => $this->user['id']]));
        }


        if (isset($param['datetime']) && $param['datetime'] != '' && strlen($param['datetime']) === 16)
        {
            $advanceTime = strtotime($param['datetime']);

            list($year, $month, $day, $hour, $minute, $is_summer) = explode(',', date('Y,m,d,H,i,I', $advanceTime));

            $advanceInfo = [
                'year' => $year * 1,
                'month' => $month * 1,
                'day' => $day * 1,
                'hour' => $hour * 1,
                'minute' => $minute * 1,
                'timezone' => $param['timezone'] * 1,
                'is_summer' => $is_summer * 1,
            ];
            if (isset($param['longitude']))
            {
                $advanceInfo['longitude'] = $param['longitude'];
            }
            if (isset($param['latitude']))
            {
                $advanceInfo['latitude'] = $param['latitude'];
            }
            $Astrolog->setAdvance($advanceInfo);
        }

        $userSetting = [$param['chart'] => $settingData];

        // 法达参数数字转为字符串
        if (isset($userSetting[$param['chart']]['firdaria']))
        {
            if ($userSetting[$param['chart']]['firdaria'] === 0)
            {
                $userSetting[$param['chart']]['firdaria'] = 'default';
            } else
            {
                $userSetting[$param['chart']]['firdaria'] = 'another';
            }
        }

        // 界主星参数数字转为字符串
        if (isset($userSetting[$param['chart']]['bound_type']))
        {
            if ($userSetting[$param['chart']]['bound_type'] === 0)
            {
                $userSetting[$param['chart']]['bound_type'] = 'default';
            } else
            {
                $userSetting[$param['chart']]['bound_type'] = 'ptolemy_world';
            }
        }

        $data = $Astrolog->setExecPrefix(config('astrolog.prefix'))
            ->setIsAstrolabe(true)
            ->setUserSetting($userSetting)
            ->setVersion(2)
            ->setGetDatas([$param['chart']])
            ->getAstroDatas()
            ->getDatas($param['chart']);

        if (in_array($param['chart'], ['now', 'natal', 'profection', 'firdaria', 'dodekatemorion', 'trimsamsa', 'ecliptic', 'fortune']))
        {
            $data['extent_type'] = $settingData['bound_type'] ?? 0;
        }

        //创建服务器请求的文件
        $resultStr = "{\"code\": 200, \"msg\": \"加载成功\", \"data\":" . json_encode($data, JSON_UNESCAPED_UNICODE) . "}";
        $this->createSettingFile($param['chart'] . '_server.json', $resultStr);

    }

    /**
     * 创建文件
     * @param string $filename 文件名称 不带目录
     * @param string $content 文件内容
     */
    protected function createSettingFile($filename = 'a.json', $content = '')
    {
        try
        {
            $settingDir = RUNTIME_PATH . 'astro';

            !is_dir($settingDir) && mkdir($settingDir, 0755, true);

            $filePath = $settingDir . DS . $filename;

            file_put_contents($filePath, $content);
        }
        catch (Exception $e)
        {
            Log::write('createSettingFile:' . $e->getMessage(), 'error');
        }
    }

}
